import {observable, action, computed} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from "../../util/reportUtil";
import {ReportStore} from "./ReportStore";
import has from 'lodash/has';

const getLabel = WeaLocaleProvider.getLabel;

export class MostOverTimeReqStore extends ReportStore {
    @observable labels = {};
    @observable advanceSearchPara = {
        datarange: 1,
        dataid: '',
        dateType: 1,
        startDate: '',
        endDate: ''
    }
    //modal
    @observable modalPara = {
        modalVisible: false,
        modalKey: '',
        modaltitle: '',
        dataParams: {}
    }

    @computed get collectParams() {
        return {
            favouritetype: 2,
            importantlevel: 1,
            favname: this.labels.reportTitle,
            link: "wui/engine.html#/main/workflow/mostOvertimeRequestAnalyse"
        }
    }

    constructor() {
        super();
        this.initDatas = this.initDatas.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.showModal = this.showModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas() {
        API_WFREPORT.getMOTRSearchCondition({}).then((data) => {
            const {noRight = false, rightKey = '-100', labels = {}, config = [], bottomInfo, reportid} = data;
            this.advanceSearchData = {noRight, config, bottomInfo, reportid};
            if (!noRight) {
                this.labels = labels;
                this.updateAdvanceSearchPara({rightKey});
            }
        });

    }

    loadAnalyseData() {
        this.loading = true;
        Promise.all([
            API_WFREPORT.getMOTRCountData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getMOTREchartData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(() => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        let iddoupdate = false;
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                iddoupdate = true;
                if (o === 'datarange' && para[o] > 1) isload = false;
                if (o === 'datarange' && para[o] == 1) {
                    defaultInfo.dataid = '';
                }
                if (o === 'dataid' && para[o] == '' && (para.datarange > 1 || (para.datarange > 1 && this.advanceSearchPara.datarange > 1))) {
                    isload = false;
                }
                if (o === 'dateType') {
                    if (para[o] == 6) {
                        isload = false;
                    } else {
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
            }
        }
        if (iddoupdate) {
            this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
            if (isload) {
                this.loadAnalyseData();
            }
        }
    }

    //type 1:更多 2:echart
    showModal(type, para = {}) {
        if (type == 1) {
            const {module, title = '', key = ''} = para;
            const dataParams = {
                'api': 'getMOTREchartMore',
                ...this.advanceSearchPara,
                module
            }
            this.modalPara = {
                modalVisible: true,
                modaltitle: this.labels[`echart${module}Title`],
                dataParams: dataParams,
                isRowClick: true,
                clickParams: {'reportDetailType': module,module}
            }
        } else if (type == 2) {
            //reportDetailType 0 :节点分析 1：流程列表 2：人员分析
            const {key, title = '', module, parentKeyid = ''} = para;
            const modalLabelSuffix = this.labels[`lebelsuffix${module}`];
            const modaltitle = module == 2 ? `${modalLabelSuffix}：${title}` : modalLabelSuffix;
            const dataParams = {
                'api': 'getMOTREchartDetailList',
                'keyid': key,
                'reportDetailType': module,
                'parentKeyid': parentKeyid,
                ...this.advanceSearchPara,
                module
            }
            const bottomBtns = [];
            let needAdvacnceSearch = false;
            if (module == 1) {
                let btnTitle = getLabel(384563, '超期节点分析');
                bottomBtns.push({reportDetailType: 0, btnTitle, module, title, parentKeyid: key,isLoadVersionTab:1});
                needAdvacnceSearch = true;
            } else {
                let btnTitle = getLabel(383635, '超期流程清单');
                bottomBtns.push({reportDetailType: 1, btnTitle, module, title, parentKeyid: key});
            }
            this.modalPara = {
                modalVisible: true,
                modaltitle: modaltitle,
                dataParams: dataParams,
                bottomBtns: bottomBtns,
                isRowClick: module == 2,
                clickParams: {'reportDetailType': 1,module},
                needAdvacnceSearch: module == 1
            }
        } else if (type == 3) {
            const dataParams = {
                'api': 'getMOTREchartDetailList',
                'reportDetailType': 1,
                ...this.advanceSearchPara
            }
            this.modalPara = {
                modalVisible: true,
                modaltitle: this.labels[`lebelsuffix1`],
                dataParams: dataParams,
                needAdvacnceSearch: true
            }
        }
    }

    updateModalPara(para = {}) {
        /**
         * 触发来源
         * 1:点击更多点击行
         * 2:点击报表图后点击列表行
         * 3：点击底部按钮
         */
        let modaltitle = '';
        let isRowClick = false;
        let clickParams = {};
        const bottomBtns = [];
        let needAdvacnceSearch = false;
        const {reportDetailType, btnTitle = '', title = '', keyid = '', parentKeyid = '', currentTime, module,isLoadVersionTab = 0} = para;
        let _parentKeyid = parentKeyid;
        _parentKeyid = _parentKeyid == '' ? keyid : `${keyid}_${_parentKeyid}`;
        if (reportDetailType == 0) {
            modaltitle = `${btnTitle}：${title}`;
        } else if (reportDetailType == 1) {
            modaltitle = getLabel(383635, '超期流程清单');
            if (keyid != '') {
                let btnTitle = getLabel(384563, '超期节点分析');
                bottomBtns.push({reportDetailType: 0, btnTitle, title, parentKeyid:_parentKeyid, module,isLoadVersionTab:1});
            }
            needAdvacnceSearch = true;
        } else if (reportDetailType == 2) {
            modaltitle = `${this.labels[`lebelsuffix2`]}：${title}`;
            isRowClick = true;
            let btnTitle = getLabel(383635, '超期流程清单');
            bottomBtns.push({reportDetailType: 1, btnTitle, title, parentKeyid:_parentKeyid, module});
            clickParams = {'reportDetailType': 1,module};
        }
        const dataParams = {
            'api': 'getMOTREchartDetailList',
            'reportDetailType': reportDetailType,
            ...this.advanceSearchPara,
            keyid,
            parentKeyid,
            module,
            currentTime,
            isLoadVersionTab
        }
        const updateParam = {modaltitle, isRowClick, dataParams, clickParams, bottomBtns, needAdvacnceSearch};
        this.modalPara = {...this.modalPara, ...updateParam};
    }

    clearStatus(){
        this.loading = false;
        this.advanceSearchPara = {
            datarange: 1,
            dataid: '',
            dateType: 1,
            startDate: '',
            endDate: ''
        };
        this.modalPara = {};
        this.advanceSearchData = {};
        this.analyseData = {};
    }
}